package cn.fotoit.product.config;


import cn.fotoit.product.util.ReturnBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class ExceptionHandleController {


    /**
     * 登录异常
     */
    @ExceptionHandler(AuthenticationException.class)
    public ReturnBean authenticationExceptionHandel(AuthenticationException e) {
        log.error(e.getMessage(), e);

        String msg;
        if (e instanceof IncorrectCredentialsException) {
            msg = "【登录密码错误】" + e.getMessage();
        } else if (e instanceof ExcessiveAttemptsException) {
            msg = "【登录失败次数过多,五分钟后再试】" + e.getMessage();
        } else if (e instanceof LockedAccountException) {
            msg = "【帐号已被锁定】" + e.getMessage();
        } else if (e instanceof DisabledAccountException) {
            msg = "【帐号已被禁用】." + e.getMessage();
        } else if (e instanceof ExpiredCredentialsException) {
            msg = "【帐号已经过期】" + e.getMessage();
        } else if (e instanceof UnknownAccountException) {
            msg = "【帐号不存在】" + e.getMessage();
        } else if (e instanceof AccountException) {
            msg = e.getMessage();
        } else {
            log.error("【未知异常,登录失败】" + e.getMessage());
            msg = "【未知异常,登录失败】" + e.getMessage();
        }
        return ReturnBean.error(msg);
    }

    @ExceptionHandler(AuthorizationException.class)
    public ReturnBean authorizationExceptionHandel(AuthorizationException e) {
        return ReturnBean.error("【权限不足】" + e.getMessage());
    }

    /**
     * 未登录异常
     */

    @ExceptionHandler(UnauthenticatedException.class)
    public ReturnBean unAuthenticationExceptionHandle(UnauthenticatedException e) {
        log.error(e.getMessage());
        return ReturnBean.error("【用户未登录或认证】");
    }

    /**
     * 权限不足异常
     */
    @ExceptionHandler(UnauthorizedException.class)
    public ReturnBean unAuthorizedExceptionHandle(UnauthorizedException e) {
        log.error(e.getMessage(), e);
        return ReturnBean.error("【权限不足】" + e.getMessage());
    }

    /**
     * 数据校检异常
     */
    @ExceptionHandler(BindException.class)
    public ReturnBean validExceptionHandle(BindException e) {
        log.error(e.getMessage(), e);
        return ReturnBean.error("【数据校检异常】" + e.getMessage());
    }

    /**
     * 数据校检异常,当参数含有注解{@code @RequestBody}时,
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ReturnBean methodArgumentNotValidExceptionHandle(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);

        return ReturnBean.error("【请求参数不全】" + e.getMessage());
    }

    /**
     * 请求参数丢失异常，当方法标记为@RequestParam(require = true)
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ReturnBean MissingServletRequestParameterExceptionHandle(MissingServletRequestParameterException e) {
        log.error(e.getMessage(), e);
        return ReturnBean.error("【请求参数不全】" + e.getMessage());
    }

    /**
     * 捕获所有异常
     */
    @ExceptionHandler(Throwable.class)
    public ReturnBean throwableHandle(Throwable e) {
        log.error(e.getMessage(), e);
        return ReturnBean.error("【服务器异常】" + e.getMessage());
    }


}
